package com.skstudio.nuprint;

import java.io.File;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.skstudio.nuprint.PsFileMonitorService.OnFilesChangelistener;
import com.skstudio.nuprint.view.PsViewActivity;

public class MainActivity extends Activity implements OnItemClickListener, OnFilesChangelistener{
	
	PsFileMonitorService mPsMonitor = new PsFileMonitorService(this);
	
	ListView mListView;
	
	DnssdService mDnssdService = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
              	
		View root = getWindow().getDecorView().findViewById(android.R.id.content);
		mListView = (ListView) root.findViewById(R.id.listView1);
		mListView.setOnItemClickListener(this);
		onFilesChanged(mPsMonitor);
		
		mDnssdService = new DnssdService(this);
		mDnssdService.StartService();
		   	
    	startService( new Intent(MainActivity.this, 
    			LpdService.class));
    	doBindService();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private boolean mIsBound = false;
    @SuppressWarnings("unused")
	private LpdService mBoundService = null;
    
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mBoundService = ((LpdService.PrintIOBinder)service).getService();          
            mIsBound = true;
        }
        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            mBoundService = null;
        }
    };
    
    void doBindService() {
        // Establish a connection with the service.  We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).
    	bindService(new Intent(this, LpdService.class),
        		mConnection, Context.BIND_AUTO_CREATE);
    }
    
    void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    @Override
    protected void onRestart () {
    	super.onRestart();	
    	Log.v("PbpMain", this.toString() + " onRestart");
    }
    
    
    @Override
    protected void onPostResume () {
    	super.onPostResume();
    	Log.v("PbpMain", this.toString() + " onPostResume");
    }
    
    @Override
    protected void onResume () {
    	super.onResume();
    	Log.v("PbpMain", this.toString() + " onResume");
    }
    
    @Override
    protected void onPause () {
    	super.onPause();
    	Log.v("PbpMain", this.toString() + " onPause");
    }

    @Override
    protected void onStop () {
    	super.onStop();
    	System.gc();
    	Log.v("PbpMain", this.toString() + " onStop");
    }
    
    @Override
    protected void onDestroy () {
    	super.onDestroy();
    	doUnbindService();
    	
    	System.gc();
    	Log.v("PbpMain", this.toString() + " onDestroy");
    }
    
    @Override
    protected void onUserLeaveHint () {
    	super.onUserLeaveHint();
    	Log.v("PbpMain", this.toString() + " onUserLeaveHint");
    }
    
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	Log.v("PbpMain", this.toString() + " onSaveInstanceState");
    	super.onSaveInstanceState(savedInstanceState);
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      Log.v("PbpMain", this.toString() + " onRestoreInstanceState");
    }

	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<File> adapter = (ArrayAdapter<File>) mListView.getAdapter();
		File file = adapter.getItem(position);
		
		Intent intent = new Intent(this, PsViewActivity.class);
		intent.putExtra("Path", file.getAbsolutePath());
		startActivity(intent);
	}

	@Override
	public void onFilesChanged(PsFileMonitorService service) {
		File[] files = service.getCurrentSnapshot();
		ArrayAdapter<File> adapter = new ArrayAdapter<File>(this,
				android.R.layout.simple_list_item_1, files);
		mListView.setAdapter(adapter);

		adapter.notifyDataSetInvalidated();
	}
}
